/**
 * @author helio
 */
var UserAuthzInputView = Ext.extend(BasicPanel,{
	
	initlize : function() {
		
		this.userPanel = {
				xtype: 'fieldset',
		        title: '选择用户',
		        autoHeight: true,
		        bodyStyle : 'background-color: transparent;', 
		        layout:'column',
		        items : [{
					columnWidth:.4,
					layout: 'form',
					border:false,
					labelWidth: 60,
					bodyStyle: 'padding-left:5px;background-color: transparent;',
					items: [new Ext.form.ComboBox({
						tpl: comboxTpl,
						fieldLabel: '用户名称',
				        store: DataStoreUtil.getLoginAuthzStore(),
				        displayField:'name',
				        typeAhead: true,
				        triggerAction: 'all',
				        selectOnFocus:true,
						name: 'name',
						listeners: {
							'select': this.userSelectedHandler.createDelegate(this)
						},						
						anchor:'90%'
				    })]
		        },{
					columnWidth:.4,
					layout: 'form',
					border:false,
					labelWidth: 70,
					bodyStyle: 'background-color: transparent;',
					items: [new Ext.form.ComboBox({
						tpl: comboxTplLoginid,
						fieldLabel: '登录用户名',
				        store: DataStoreUtil.getLoginAuthzStore(),
				        displayField: 'loginid',
				        typeAhead: true,
				        mode: 'local',
				        triggerAction: 'all',
				        selectOnFocus:true,
						name: 'loginid',
						listeners: {
							'select': this.userSelectedHandler.createDelegate(this)
						},
						allowBlank: false,
						anchor:'90%'					
				    })]
		        },{
					columnWidth:.2,
					layout: 'form',
					border:false,
					labelWidth: 30,
					bodyStyle: 'background-color: transparent;',
					items: [new Ext.form.ComboBox({
						tpl: comboxTpl,
						fieldLabel: '角色',
				        store: DataStoreUtil.getLoginRoleStore(),
				        displayField:'name',
				        typeAhead: true,
				        mode: 'local',
				        forceSelection: false,
				        triggerAction: 'all',
				        selectOnFocus:true,
						name: 'role',
						listeners: {
							'select': function(combox, record){
								this.checkGroup.setValue(record.data.authz);
	        				}.createDelegate(this)
						},
						anchor:'95%'
				    })]
		        }]
		}
		
		/*====================================================================
	     * Authz Panel
	     *====================================================================*/
	    this.checkGroup = {
	        xtype: 'fieldset',
	        title: '选择该用户可以操作的模块',
	        autoHeight: true,
	        bodyStyle : 'background-color: transparent', 
	        layout: 'form',
	        items: [{
	            xtype: 'checkboxgroup',
	            fieldLabel: '销售业务管理',
	            columns: 4,
	            items: [
	                {boxLabel: '新增销售单', name: 'authz', inputValue: 10},
	                {boxLabel: '销售单查询/确认', name: 'authz', inputValue: 11},
	                {boxLabel: '收款单', name: 'authz', inputValue: 12},
	                {boxLabel: '收款单查询', name: 'authz', inputValue: 13},
	                {boxLabel: '销售退货单', name: 'authz', inputValue: 14},
	                {boxLabel: '销售退货单查询', name: 'authz', inputValue: 15}	                
	            ]
	        },{
	            xtype: 'checkboxgroup',
	            fieldLabel: '销售统计分析',
	            itemCls: 'x-check-group-alt',
	            items: [
	                {boxLabel: '日销售状况', name: 'authz', inputValue: 20},
	                {boxLabel: '月销售状况', name: 'authz', inputValue: 21},
	                {boxLabel: '销售排行榜', name: 'authz', inputValue: 22}
	            ]
	        },{
	            xtype: 'checkboxgroup',
	            fieldLabel: '采购业务管理',
	            columns: 4,
	            items: [
	                {boxLabel: '新增采购单', name: 'authz', inputValue: 30},
	                {boxLabel: '采购单查询/确认', name: 'authz', inputValue: 31},
	                {boxLabel: '付款单', name: 'authz', inputValue: 32},
	                {boxLabel: '付款单查询', name: 'authz', inputValue: 33},
	                {boxLabel: '采购退货单', name: 'authz', inputValue: 34},
	                {boxLabel: '采购退货单查询', name: 'authz', inputValue: 35}
	            ]
	        },{
	            xtype: 'checkboxgroup',
	            fieldLabel: '库存管理',
	            itemCls: 'x-check-group-alt',
	            columns: 4,
	            items: [
	                {boxLabel: '商品调拨', name: 'authz', inputValue: 40},
	                {boxLabel: '调拨单查询', name: 'authz', inputValue: 41},
	                {boxLabel: '接收入库', name: 'authz', inputValue: 42},
	                {boxLabel: '检查库存', name: 'authz', inputValue: 43},
	                {boxLabel: '库存商品管理', name: 'authz', inputValue: 44}
	            ]
	        },{
	            xtype: 'checkboxgroup',
	            fieldLabel: '钱帐管理',
	            columns: 4,
	            items: [
	                {boxLabel: '一般费用', name: 'authz', inputValue: 50},
	                {boxLabel: '其它收入', name: 'authz', inputValue: 51},
	                {boxLabel: '收款单', name: 'authz', inputValue: 52},
	                {boxLabel: '付款单', name: 'authz', inputValue: 53},
	                {boxLabel: '总账薄', name: 'authz', inputValue: 54},
	                {boxLabel: '现金银行', name: 'authz', inputValue: 55}
	            ]
	        },{
	            xtype: 'checkboxgroup',
	            fieldLabel: '基本数据编辑',
	            itemCls: 'x-check-group-alt',
	            columns: 4,
	            items: [
	                {boxLabel: '部门信息', name: 'authz', inputValue: 60},
	                {boxLabel: '商品类别', name: 'authz', inputValue: 61},
	                {boxLabel: '厂商资料', name: 'authz', inputValue: 62},
	                {boxLabel: '客户档案', name: 'authz', inputValue: 63},
	                {boxLabel: '存货仓库', name: 'authz', inputValue: 64},
	                {boxLabel: '内部职员', name: 'authz', inputValue: 65},
	                {boxLabel: '银行账户', name: 'authz', inputValue: 66},
	                {boxLabel: '科目类别', name: 'authz', inputValue: 67}
	            ]
	        },{
	            xtype: 'checkboxgroup',
	            fieldLabel: '系统管理',
	            items: [
	                {boxLabel: '修改密码', name: 'authz', inputValue: 70},
	                {boxLabel: '权限设置', name: 'authz', inputValue: 71},
	                {boxLabel: '发布新闻', name: 'authz', inputValue: 72}
	            ]
	        }],
	        
	        setValue: function(value) {
				var radiogroup = Ext.query("*[name=authz]");
				if(radiogroup) {
					Ext.each(radiogroup,function(f){
			　　　　　　　 	if(value.indexOf(f.value) != -1 || value=="*"){　　
			　　　　　　　 		f.checked = true;　　
			　　　　　　　 	}else{　　
			　　　　　　　　　　　 	f.checked = false;　
			　　　　　　　 	}　　
			　　		});
				}
			},
			
			getValue: function() {				
				var re = "";
				var radiogroup = Ext.query("*[name=authz]");
				if(radiogroup) {
					Ext.each(radiogroup,function(f){
			　　　　　　　 	if(f.checked){　　
			　　　　　　　 		re += f.value + ",";
			　　　　　　　 	}　　
			　　		});
				}
				return re.substr(0,re.length - 1);
			}
	    };

	
		if(!this.inputForm) {
			this.inputForm = new Ext.FormPanel({
				region: 'center',
		        labelWidth: 100,
		        border: false,
		        bodyStyle : 'background-color: transparent;padding:10px', 
		        items: [this.userPanel,this.checkGroup],
		        buttons: [{
		            text: '保存',
		            handler: function(){
		        		if(this.inputForm.form.isValid()) {
			        		this.params = this.inputForm.form.getValues();
			        		if(this.selectedUser) {
			        			this.params['id'] = this.selectedUser.id;
			        		}
			        		
			        		SALE.Action.userAuthzSave(this.saveSuccessHandler.createDelegate(this),this.params);
		        		}
		            }.createDelegate(this)
		        },{
		            text: '取消',
		            handler: function(){		        		
		        		this.inputForm.form.reset();
		            }.createDelegate(this)
		        }]
		    });

		}
			
	},
	
	getCorePanel : function() {
		this.initlize();
		var corePanel = new Ext.Panel({
	        title: '用户使用权限分配',
			border: false,
			layout: 'border',
			
			items: [this.inputForm]
		});	
		
		return corePanel;
	},
	
	userSelectedHandler : function(combox, record) {
		this.selectedUser = record.data;
		this.inputForm.form.findField('name').setValue(record.data.name);
		this.inputForm.form.findField('loginid').setValue(record.data.loginid);
		this.inputForm.form.findField('role').setValue(record.data.role);
		
		if(record.data.authz) {
			this.checkGroup.setValue(record.data.authz);
		}
	}

});

UserAuthzInputView.showInputPanel = function() {	
	var inputView = new UserAuthzInputView();
	SALE.App.showOperateTabPanel(inputView.getCorePanel(), "权限设置");
}
